﻿using DTO;
using Model;
using Model.Modules;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utility;



namespace Proxy
{
    public class ESDataHelper
    {
        ElasticsearchHelper eshelper;

        public ESDataHelper() {

            eshelper = new ElasticsearchHelper();

        }

        #region 按批次查询
        public ObservableCollection<image> GetImagesByMainID(string main_id) 
        {
            var images = new ObservableCollection<image>();
            try
            {
                Dictionary<string, object> querydic = new Dictionary<string, object>();
                querydic.Add("main_id", main_id);
                var result = eshelper.Search<image>("image_table", querydic, null, 8000, null);
                if(result.Count>0)
                    images =new ObservableCollection<image>(result);
               
            }
            catch (Exception e) {
                Logger.Debug(e.Message);
            }
            return images;
        }

        public ObservableCollection<steel_defect> GetSteelDefectsByMainID(string main_id)
        {
            var defects = new ObservableCollection<steel_defect>();
            try
            {
                Dictionary<string, object> querydic = new Dictionary<string, object>();
                querydic.Add("main_id", main_id);
                Dictionary<string, bool> sortdic = new Dictionary<string, bool>();

                var result = eshelper.Search<steel_defect>("steel_defect", querydic, null, 8000, null);
                if (result.Count > 0)
                    defects = new ObservableCollection<steel_defect>(result);
            }
            catch (Exception e)
            {
                Logger.Debug(e.Message);
            }
            return defects;
        }


        public ObservableCollection<image_defect> GetDefectsByMainID(string main_id)
        {
            var defects = new ObservableCollection<image_defect>();
            try
            {
                Dictionary<string, object> querydic = new Dictionary<string, object>();
                querydic.Add("main_id", main_id);
                Dictionary<string, bool> sortdic = new Dictionary<string, bool>();
                var result = eshelper.Search<image_defect>("image_defect_table", querydic, null, 8000, null);
                if (result.Count > 0)
                    defects = new ObservableCollection<image_defect>(result);
            }
            catch (Exception e)
            {
                Logger.Debug(e.Message);
            }
            return defects;
        }

        #endregion

        #region 按时间区间查询
        public ObservableCollection<image> GetImagesDuringDate(DateTime start, DateTime end)
        {
            var images = new ObservableCollection<image>();
            try
            {
                Dictionary<string, object> querydic = new Dictionary<string, object>();
                querydic.Add("insert_time", new List<DateTime?>() { start, end });
                var result = eshelper.Search<image>("image_table", querydic, null, 8000, null);
                if (result.Count > 0)
                    images = new ObservableCollection<image>(result);

            }
            catch (Exception e)
            {
                Logger.Debug(e.Message);
            }
            return images;
        }

        public ObservableCollection<steel_defect> GetSteelDefectsDuringDate(DateTime start, DateTime end,int from=0)
        {
            var defects = new ObservableCollection<steel_defect>();
            try
            {
                Dictionary<string, object> querydic = new Dictionary<string, object>();
                querydic.Add("insert_time", new List<DateTime?>() { start, end });
                Dictionary<string, bool> sortdic = new Dictionary<string, bool>();
                sortdic.Add("insert_time", true);
                var result = eshelper.Search<steel_defect>("steel_defect", querydic, sortdic, 8000, from);
                if (result.Count > 0)
                    defects = new ObservableCollection<steel_defect>(result);
            }
            catch (Exception e)
            {
                Logger.Debug(e.Message);
            }
            return defects;
        }


        public ObservableCollection<image_defect> GetDefectsDuringDate(DateTime start, DateTime end)
        {
            var defects = new ObservableCollection<image_defect>();
            try
            {
                Dictionary<string, object> querydic = new Dictionary<string, object>();
                querydic.Add("insert_time", new List<DateTime?>() { start, end });
                Dictionary<string, bool> sortdic = new Dictionary<string, bool>();
                sortdic.Add("insert_time", true);
                var result = eshelper.Search<image_defect>("image_defect_table", querydic, sortdic, 8000, null);
                if (result.Count > 0)
                    defects = new ObservableCollection<image_defect>(result);
            }
            catch (Exception e)
            {
                Logger.Debug(e.Message);
            }
            return defects;
        }

        #endregion

        #region 按条件查询
        public ObservableCollection<image> GetImagesByIDs(string[] ids)
        {
            var images = new ObservableCollection<image>();
            try
            {
                Dictionary<string, object> querydic = new Dictionary<string, object>();
                querydic.Add("id", ids);
                var result = eshelper.Search<image>("image_table", querydic, null, 8000, null);
                if (result.Count > 0)
                    images = new ObservableCollection<image>(result);

            }
            catch (Exception e)
            {
                Logger.Debug(e.Message);
            }
            return images;
        }

        public ObservableCollection<image_defect> GetDefectsByIDs(string[] ids)
        {
            var images = new ObservableCollection<image_defect>();
            try
            {
                Dictionary<string, object> querydic = new Dictionary<string, object>();
                querydic.Add("id", ids);
                var result = eshelper.Search<image_defect>("image_defect_table", querydic, null, 8000, null);
                if (result.Count > 0)
                    images = new ObservableCollection<image_defect>(result);

            }
            catch (Exception e)
            {
                Logger.Debug(e.Message);
            }
            return images;
        }



        #endregion

        #region 查询最新数据
        public ObservableCollection<image> GetNewImagesByMainID(string main_id, DateTime latestInserttime)
        {
            var images = new ObservableCollection<image>();
            try
            {
                Dictionary<string, object> querydic = new Dictionary<string, object>();
                querydic.Add("main_id", main_id);
                querydic.Add("insert_time", new List<DateTime?>() {latestInserttime,null});
                var result = eshelper.Search<image>("image_table", querydic, null, 8000, null);
                if (result.Count > 0)
                    images = new ObservableCollection<image>(result);
            }
            catch (Exception e)
            {
                Logger.Debug(e.Message);
            }
            return images;
        }
        public ObservableCollection<image> GetNewImages(int cameraid, DateTime? from, DateTime? to,int size)
        {
            var images = new ObservableCollection<image>();
            try
            {
                Dictionary<string, object> querydic = new Dictionary<string, object>();
                
                querydic.Add("insert_time", new List<DateTime?>() { from, to });
                querydic.Add("camera_id", cameraid);

                Dictionary<string, bool> sortlist = new Dictionary<string, bool>();
                sortlist.Add("insert_time", true);

                var result = eshelper.Search<image>("image_table", querydic, sortlist, size, null);
                if (result.Count > 0)
                    images = new ObservableCollection<image>(result.OrderByDescending(t=>t.insert_time));
            }
            catch (Exception e)
            {
                Logger.Debug(e.Message);
            }
            return images;
        }

        public ObservableCollection<image> GetNewImages(int cameraid,  int size)
        {
            var images = new ObservableCollection<image>();
            try
            {
                Dictionary<string, object> querydic = new Dictionary<string, object>();
                querydic.Add("camera_id", cameraid);
                Dictionary<string, bool> sortlist = new Dictionary<string, bool>();
                sortlist.Add("insert_time", true);
                var result = eshelper.Search<image>("image_table", querydic, sortlist, size, null);
                
                if (result.Count > 0)
                    images = new ObservableCollection<image>(result.OrderByDescending(t => t.insert_time));
            }
            catch (Exception e)
            {
                Logger.Debug(e.Message);
            }
            return images;
        }

        public ObservableCollection<steel_defect> GetNewSteelDefects(int size)
        {
            var defects = new ObservableCollection<steel_defect>();
            try
            {

                Dictionary<string, bool> sortdic = new Dictionary<string, bool>();
                sortdic.Add("insert_time", true);
                var result = eshelper.Search<steel_defect>("steel_defect", null, sortdic, size, null);
                if (result.Count > 0)
                    defects = new ObservableCollection<steel_defect>(result);
            }
            catch (Exception e)
            {
                Logger.Debug(e.Message);
            }
            return defects;
        }


        public DateTime getNewestImageInserTime(int cameraid) {

            Dictionary<string, object> querydic = new Dictionary<string, object>();
            querydic.Add("camera_id", cameraid);

            Dictionary<string, bool> sortlist = new Dictionary<string, bool>();
            sortlist.Add("insert_time", true);
            var result = eshelper.Search<image>("image_table", null, sortlist, 1, null);

            return result.First().insert_time;

        }

        #endregion

        #region 聚合查询

        //public ObservableCollection<SteelDefectItemCount> GetSteelDefectItemCountList(DateTime start,DateTime end) {
        //    if (start > end)
        //        return null;
        //    var list =new ObservableCollection<SteelDefectItemCount>();
        //    for(DateTime dt =new DateTime(start.Year,start.Month,start.Day,start.Hour,0,0); dt <= new DateTime(end.Year, end.Month, end.Day, end.Hour, 0, 0); dt=dt.AddHours(1))
        //    {
        //        list.Add(new SteelDefectItemCount() { Date=dt });
        //    }
        //    var querydic = new Dictionary<string, object>();
        //    querydic.Add("alert_type", "警告");
        //    querydic.Add("insert_time", new List<DateTime?>() { start,end });
        //    var sortdic = new Dictionary<string, bool>();
        //    sortdic.Add("insert_time", false);
        //    var aggrdic = new Dictionary<string, string>();
        //    aggrdic.Add("Hour", "insert_time");
        //    var result=eshelper.SearchDateHistogramAggs<steel_defect>("steel_defect", querydic, sortdic, aggrdic);
        //    foreach(var item in result)
        //    {
        //        list.Where(t => t.Date == item.Key).FirstOrDefault().Warning = item.Value;
        //    }

        //    querydic["alert_type"] = "报警";
        //    result = eshelper.SearchDateHistogramAggs<steel_defect>("steel_defect", querydic, sortdic, aggrdic);
            
        //    foreach (var item in result)
        //    {
        //        list.Where(t => t.Date == item.Key).FirstOrDefault().Alert = item.Value;
        //    }
        //    return list;

        //}

        #endregion
    }
}
